package com.yalu.service.data.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import com.yalu.dao.data.ReasonsDao;
import com.yalu.entity.base.BaseEntity;
import com.yalu.entity.data.Reasons;
import com.yalu.entity.data.ReasonsKey;
import com.yalu.service.base.impl.BaseServiceImpl;
import com.yalu.service.data.ReasonsService;
import com.yalu.util.BaseUtil;

@Service
public class ReasonsServiceImpl extends BaseServiceImpl<Reasons, ReasonsKey>
implements ReasonsService {

	@Resource
	private ReasonsDao reasonsDao;

	@Resource
	public void setBaseDao(ReasonsDao reasonsDao) {
		super.setBaseDao(reasonsDao);
	}

	@Override
	public int[] save(List<BaseEntity> entityList) {
		int[] returnCounts = new int[2];
		int saveCount = 0;
		int updateCount = 0;
		String time = BaseUtil.getTime();
		for (BaseEntity baseEntity : entityList) {

			Reasons entity = (Reasons)baseEntity;
			ReasonsKey key  = entity.getReasonsKey();
			Reasons temp = reasonsDao.get(key);
			if(temp==null){
				entity.setCreatedt(time);
				reasonsDao.save(entity);
				saveCount++;
			} else{
				BeanUtils.copyProperties(entity,temp, new String[] { "createdt", "createby"});
				reasonsDao.update(temp);
				updateCount++;
			}
		}

		returnCounts[0] = saveCount;
		returnCounts[1] = updateCount;
		return returnCounts;
	}

}
